package com.mephi.oshs.presenter;

import com.mephi.oshs.model.entry.IEntry;
import com.mephi.oshs.model.tree.OshsTree;
import com.mephi.oshs.view.api.IOshsView;
import org.eclipse.swt.widgets.EntryTreeItem;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class OshsPresenter extends AbstractPresenter {
    private static final Logger logger = LoggerFactory.getLogger(OshsPresenter.class);

    private final List<IEntry> quickSearchEntryList = new ArrayList<IEntry>();

    private IEntry selectedEntry = null;

    private String quickSearchQuery = "";
    private int quickSearchEntryIndex = 0;
    private int quickSearchEntryPosition = 0;

    @Override
    public void bind() {
        final IOshsView oshsView = super.getOshsView();

        oshsView.getQuickSearchAction().addClickListener(new Listener() {

            @Override
            public void handleEvent(Event event) {
                onQuickSearch();
            }
        });
        oshsView.getOshsTreeWidget().addSelectListener(new Listener() {

            @Override
            public void handleEvent(Event event) {
                onSelectTreeItem(event);
            }
        });
    }

    @Override
    public void unbind() {
        throw new RuntimeException("unbind(): Not implemented yet.");
    }

    public IEntry getSelectedEntry() {
        return selectedEntry;
    }

    private void onQuickSearch() {
        final OshsTree<IEntry> oshsTree = OshsTree.getInstance();
        final String searchQuery = this.getOshsView().getQuickSearchText().getText();

        // New search request.
        if (!this.quickSearchQuery.equals(searchQuery)) {
            this.quickSearchQuery = searchQuery;
            this.quickSearchEntryIndex = 0;
            this.quickSearchEntryPosition = 0;
            this.quickSearchEntryList.clear();
            this.quickSearchEntryList.addAll(oshsTree.quickSearch(searchQuery));
        }

        // Select next entry.
        while (this.quickSearchEntryIndex < this.quickSearchEntryList.size()) {
            if (this.getOshsView().getOshsTreeWidget().selectEntry(quickSearchEntryList.get(this.quickSearchEntryIndex),
                    this.quickSearchEntryPosition)) {
                this.quickSearchEntryPosition++;
                return;
            } else {
                this.quickSearchEntryIndex++;
                this.quickSearchEntryPosition = 0;
            }
        }

        // Restore indexes - return to the first entry.
        this.quickSearchEntryIndex = 0;
        this.quickSearchEntryPosition = 0;
        this.getOshsView().getOshsTreeWidget().deselectAll();

        logger.debug("Quick search found nothing.");
    }

    private void onSelectTreeItem(Event event) {
        this.selectedEntry = ((EntryTreeItem) event.item).getEntry();
        this.getExploreView().getShortNameTextField().setText(getSelectedEntry().getShortName());

        this.getExploreView().getAttributesTable().setTable(getSelectedEntry().getAttributes());
    }
}
